package com.ruin.tech.ahri.controller;

import com.ruin.tech.ahri.model.dto.Book;
import com.ruin.tech.ahri.model.dto.BookDTO;
import com.ruin.tech.ahri.result.Result;
import com.ruin.tech.ahri.result.ResultCode;
import com.ruin.tech.ahri.result.ResultFactory;
import com.ruin.tech.ahri.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program ahri
 * @description: 预约控制器
 * @author: ruin
 * @create: 2019/12/26 09:34
 */

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    IBookService bookService;

    @PostMapping(value = "/add", produces = "application/json;charset=utf-8")
    public Result add(@RequestBody Book book) throws Exception {
        try{
            bookService.add(book);
        }catch (Exception e){
            log.error("预约失败："+book.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.BOOK_FAIL);
        }
        return ResultFactory.buildSuccessResult(null);
    }

    @PostMapping(value = "/findPage", produces = "application/json;charset=utf-8")
    public Result findPage(@RequestBody BookDTO bookDTO) throws Exception {
        try{
            List<Book> books = bookService.findPage(bookDTO);
            return ResultFactory.buildSuccessResult(books);
        }catch (Exception e){
            log.error("获取当前预约信息失败："+bookDTO.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.FIND_BOOKS_FAIL);
        }
    }

    @PostMapping(value = "/del", produces = "application/json;charset=utf-8")
    public Result del(@RequestBody BookDTO bookDTO) throws Exception {
        try{
            bookService.del(bookDTO);
            return ResultFactory.buildSuccessResult(null);
        }catch (Exception e){
            log.error("预约记录删除失败："+bookDTO.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.DEL_BOOKS_FAIL);
        }
    }

    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    public Result edit(@RequestBody Book book) throws Exception {
        try{
            bookService.edit(book);
            return ResultFactory.buildSuccessResult(null);
        }catch (Exception e){
            log.error("预约记录更新失败："+book.toString(),e);
            return ResultFactory.buildFailResult(ResultCode.EDIT_BOOKS_FAIL);
        }
    }

}
